#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"


void LocalScene::OnInit()
{
	mCamera.SetPosition(glm::vec3(0.f, 0.04f, 7.f));
	mCamera.LookAt(glm::vec3(0.f, 0.04f, 7.f), glm::vec3(0.f, 0.04f, 0.f));
	mCamera.UpdateViewMatrix();

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");
	mRayTracingProcess = GetProcess<RayTracingProcess>("RayTracingProcess");

	GeometryGenerator geometryGenerator;

	GeometryGenerator::MeshData lightData = geometryGenerator.CreatePanel(1.f, 1.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* lightEntity = CreateEntity<LocalEntity>("light");
	lightEntity->AddComponent<Model>(lightData);
	lightEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 1.9f, 0.f));
	lightEntity->GetTransform().UpdateMatrices();
	lightEntity->SetColor(glm::vec4(1.f));

	lightEntity->mMaterial.isLight = 1.f;
	lightEntity->mMaterial.color = glm::vec3(50.f);


	GeometryGenerator::MeshData leftPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(-1.f, 0.f, 0.f));

	LocalEntity* leftPanelEntity = CreateEntity<LocalEntity>("leftPanel");
	leftPanelEntity->AddComponent<Model>(leftPanelData);
	leftPanelEntity->GetTransform().SetLocalPosition(glm::vec3(2.f, 0.f, 0.f));
	leftPanelEntity->GetTransform().UpdateMatrices();
	leftPanelEntity->SetColor(glm::vec4(0.65f, 0.05f, 0.05f, 1.f));

	leftPanelEntity->mMaterial.color = glm::vec3(0.65f, 0.05f, 0.05f);

	//leftPanelEntity->mMaterial.specularRate = 0.f;
	//leftPanelEntity->mMaterial.refractRate = 1.f;
	//leftPanelEntity->mMaterial.refractAngle = 0.5f;// Math::M_PI * 0.f;
	//leftPanelEntity->mMaterial.refractRoughness = 0.f;


	GeometryGenerator::MeshData rightPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(1.f, 0.f, 0.f));

	LocalEntity* rightPanelEntity = CreateEntity<LocalEntity>("rightPanel");
	rightPanelEntity->AddComponent<Model>(rightPanelData);
	rightPanelEntity->GetTransform().SetLocalPosition(glm::vec3(-2.f, 0.f, 0.f));
	rightPanelEntity->GetTransform().UpdateMatrices();
	rightPanelEntity->SetColor(glm::vec4(0.12f, 0.45f, 0.15f, 1.f));

	rightPanelEntity->mMaterial.color = glm::vec3(0.12f, 0.45f, 0.15f);


	GeometryGenerator::MeshData topPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* topPanelEntity = CreateEntity<LocalEntity>("topPanel");
	topPanelEntity->AddComponent<Model>(topPanelData);
	topPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 2.f, 0.f));
	topPanelEntity->GetTransform().UpdateMatrices();
	topPanelEntity->SetColor(glm::vec4(0.72f, 0.84f, 0.96f, 1.f));

	topPanelEntity->mMaterial.color = glm::vec3(0.72f, 0.84f, 0.96f);


	GeometryGenerator::MeshData bottomPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, 1.f, 0.f));

	LocalEntity* bottomPanelEntity = CreateEntity<LocalEntity>("bottomPanel");
	bottomPanelEntity->AddComponent<Model>(bottomPanelData);
	bottomPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -2.f, 0.f));
	bottomPanelEntity->GetTransform().UpdateMatrices();
	bottomPanelEntity->SetColor(glm::vec4(0.90f, 0.72f, 0.88f, 1.f));

	bottomPanelEntity->mMaterial.color = glm::vec3(0.90f, 0.72f, 0.88f);


	GeometryGenerator::MeshData backPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, 0.f, 1.f));

	LocalEntity* backPanelEntity = CreateEntity<LocalEntity>("backPanel");
	backPanelEntity->AddComponent<Model>(backPanelData);
	backPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.f, -2.f));
	backPanelEntity->GetTransform().UpdateMatrices();
	backPanelEntity->SetColor(glm::vec4(0.85f, 0.85f, 0.85f, 1.f));

	backPanelEntity->mMaterial.color = glm::vec3(0.85f, 0.85f, 0.f);


	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.8f, 20, 20);

	LocalEntity* reflectEntity = CreateEntity<LocalEntity>("reflect");
	reflectEntity->AddComponent<Model>(sphereData);
	reflectEntity->GetTransform().SetLocalPosition(glm::vec3(-1.f, 0.8f, 0.2f));
	// reflectEntity->GetTransform().SetLocalPosition(glm::vec3(-0.6f, 0.8f, -0.5f));
	reflectEntity->GetTransform().UpdateMatrices();
	reflectEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	reflectEntity->mMaterial.color = glm::vec3(1.f, 0.f, 0.f);

	//reflectEntity->mMaterial.specularRate = 1.f;
	//reflectEntity->mMaterial.roughness = 0.1f;

	reflectEntity->mMaterial.specularRate = 0.f;
	reflectEntity->mMaterial.refractRate = 1.f;
	reflectEntity->mMaterial.refractIdx = 2.f;
	reflectEntity->mMaterial.roughness = 0.f;

	LocalEntity* samplerEntity = CreateEntity<LocalEntity>("sampler");
	samplerEntity->AddComponent<Model>(sphereData);
	samplerEntity->GetTransform().SetLocalPosition(glm::vec3(-1.f, -1.2f, -0.2f));
	// samplerEntity->GetTransform().SetLocalPosition(glm::vec3(-1.f, -1.2f, 0.7f));
	samplerEntity->GetTransform().UpdateMatrices();
	samplerEntity->SetColor(glm::vec4(0.f, 1.f, 0.f, 1.f));

	samplerEntity->mMaterial.color = glm::vec3(0.f, 1.f, 0.f);

	samplerEntity->mMaterial.specularRate = 1.f;
	samplerEntity->mMaterial.roughness = 0.1f;

	//samplerEntity->mMaterial.specularRate = 0.f;
	//samplerEntity->mMaterial.refractRate = 1.f;
	//samplerEntity->mMaterial.refractIdx = 2.f;
	//samplerEntity->mMaterial.roughness = 0.f;

	GeometryGenerator::MeshData cubeData = geometryGenerator.CreateCube(1.2f, 2.2f, 1.2f, 1);

	LocalEntity* cubeEntity = CreateEntity<LocalEntity>("cube");
	cubeEntity->AddComponent<Model>(cubeData);
	cubeEntity->GetTransform().SetLocalPosition(glm::vec3(0.8f, -0.9f, -0.4f));
	cubeEntity->GetTransform().SetLocalOrientation(glm::quat(glm::vec3(0.f, glm::radians(60.f), 0.f)));
	cubeEntity->GetTransform().UpdateMatrices();
	cubeEntity->SetColor(glm::vec4(0.f, 0.f, 1.f, 1.f));

	cubeEntity->mMaterial.color = glm::vec3(0.f, 0.f, 1.f);
	cubeEntity->mMaterial.textureId = 1;

	//cubeEntity->mMaterial.specularRate = 0.f;
	//cubeEntity->mMaterial.refractRate = 1.f;
	//cubeEntity->mMaterial.refractIdx = 2.f;
	//cubeEntity->mMaterial.roughness = 0.f;
}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	// GameScene::OnImgui();
	// ImGui::ShowDemoWindow();

	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d ", GRenderState->GetFps());

		ImGui::Text("Spp : %d ", mRayTracingProcess->mFrame);

		ImGui::SetNextItemWidth(100);
		if (ImGui::InputInt("Max Frame", &mRayTracingProcess->mMaxFrame, 100, 100)) { mRayTracingProcess->mFrame = 0; };

		ImGui::Text("Random : ");

		if (ImGui::RadioButton("Lcg", &mRayTracingProcess->mDebugMode, 0)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Sobol", &mRayTracingProcess->mDebugMode, 1)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Hamm", &mRayTracingProcess->mDebugMode, 2)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Sobol R", &mRayTracingProcess->mDebugMode, 3)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Hamm R", &mRayTracingProcess->mDebugMode, 4)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Sobol L", &mRayTracingProcess->mDebugMode, 5)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Hamm L", &mRayTracingProcess->mDebugMode, 6)) { mRayTracingProcess->mFrame = 0; }

		ImGui::EndMainMenuBar();
	}

#if 0

	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);

	ImGui::Begin("MapOption");



	ImGui::End();

#endif
	}
